<template>
  <div class="p-6">
    <!-- 返回按钮 -->
    <el-button type="default" @click="goBack" class="mb-4">
      返回
    </el-button>

    <el-card v-if="paper">
      <h2 class="text-xl font-bold mb-4">试卷名称：{{ paper.name }}</h2>
      <p><strong>所属学科：</strong> {{ paper.subject }}</p>
      <p><strong>题目列表：</strong></p>
      <ul class="list-disc ml-6 mt-2">
        <li v-for="(q, idx) in paper.tiTle" :key="idx">{{ q }}</li>
      </ul>
    </el-card>
    <el-empty v-else description="加载中..." />
  </div>
</template>

<script setup>
import { ref, onMounted } from "vue";
import { useRoute, useRouter } from "vue-router"; // 👈 引入 useRouter
import { getPaperDetail } from "../api/paper";
import { ElMessage } from "element-plus";

const route = useRoute();
const router = useRouter(); // 👈 获取 router 实例
const paper = ref(null);

const fetchDetail = async () => {
  const name = route.params.name;
  const res = await getPaperDetail(name);
  if (res.code === 200) {
    paper.value = res.data;
  } else {
    ElMessage.error("获取试卷详情失败");
  }
};

const goBack = () => {
  router.back(); // 👈 返回上一页
};

onMounted(() => {
  fetchDetail();
});
</script>
